package com.zfy.cmsservice.controller;


import com.zfy.cmsservice.entity.Classes;
import com.zfy.cmsservice.entity.vo.CascaderClassVo;
import com.zfy.cmsservice.service.ClassesService;
import com.zfy.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-11-11
 */
@RestController
@RequestMapping("/cmsservice/classes")
@Api(description = "班级管理-班级信息维护")
public class ClassesController {
    @Autowired
    private ClassesService classesService;

    // 分页查询班级 - 支持根据名称,年级检索
    @ApiOperation(value = "分页查询班级")
    @PostMapping("pageClasses/{page}/{size}")
    public R pageClasses(@PathVariable long page, @PathVariable long size,
                         @RequestParam(required = false) Integer grade) {
        return classesService.pageClasses(page,size,grade);
    }
    // 添加班级
    @ApiOperation(value = "添加班级")
    @PostMapping("addClasses")
    public R addClasses(@RequestBody Classes classes){
        boolean flag = classesService.save(classes);
        if(flag) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }
    // 修改班级信息
    @ApiOperation(value = "修改班级信息")
    @PostMapping("updateClasses")
    public R updateTeacher(@RequestBody Classes classes){
        if (StringUtils.isEmpty(classes)) {
            // 传参错误异常
            return R.httpError();
        }
        boolean update = classesService.updateById(classes);
        if(update) {
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    // 逻辑删除班级信息
    @ApiOperation(value = "逻辑删除班级信息")
    @PostMapping("removeClasses/{id}")
    public R removeClasses(@PathVariable String id){
        boolean remove = classesService.removeById(id);
        if(remove) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    //根据id查询班級
    @ApiOperation(value = "根据id查询班級")
    @GetMapping("getClass/{id}")
    public R getClassById(@PathVariable String id) {
        Classes byId = classesService.getById(id);
        return R.ok().data("item", byId);
    }

    //  为联级选择器提供接口 选择班级
    @ApiOperation(value = "联级选择器选班級")
    @GetMapping("cascaderClass")
    public R cascaderClass() {

        List<CascaderClassVo> content = classesService.cascaderClass();
        return R.ok().data("content", content);
    }

}

